package com.hjc.business.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: HJC
 * Date: 2019/11/20
 * Time: 16:27
 * To change this template use File | Settings | File Templates.
 */
public class DBUtil {

    static Connection conn;
    static PreparedStatement pst;

    static String driver;
    static String url;
    static String username;
    static String password;

    static{
        driver="com.mysql.cj.jdbc.Driver";
        url="jdbc:mysql://localhost:3306/community?serverTimezone=GMT%2B8";
        username="root";
        password="123456";
    }

    public static PreparedStatement getPst(String sql){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            pst = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return pst;
    }

    public static void close(ResultSet rs){
        try {
            if (rs!=null) rs.close();
            if (pst!=null) pst.close();
            if (conn!=null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
